import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
import java.util.concurrent.TimeUnit;
//查找讨论组

public class DemoTest {

    public static void main(String[] args) throws Exception {

        // 设置 Appium 客户端和系统参数
        DesiredCapabilities caps = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "127.0.0.1:62001");
        caps.setCapability("platformName", "Android");
        caps.setCapability("appPackage", "com.cnezsoft.xxm");
        caps.setCapability("appActivity", ".activity.MainActivity");
        caps.setCapability("noReset", true);

        // 创建与 Appium Server 的连接
        AppiumDriver<MobileElement> driver = new AndroidDriver<>(new URL("http://localhost:4723/wd/hub"), caps);

        // 设置全局等待时长
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        // 定位到“通讯录”页面
        MobileElement contactsTab = driver.findElement(By.id("com.cnezsoft.xxm:id/main_tab_contact"));
        contactsTab.click();

        // 定位到“讨论组”标签
        MobileElement groupTab = driver.findElement(By.xpath("//android.view.ViewGroup[@content-desc=\"讨论组\"]"));
        groupTab.click();

        // 定位到搜索框并输入关键词
        MobileElement searchBox = driver.findElement(By.id("com.cnezsoft.xxm:id/search_src_text"));
        searchBox.sendKeys("三人小队");

        // 定位到搜索结果并点击
        MobileElement searchResult = driver.findElement(By.xpath("//android.widget.TextView[@text=\"三人小队\"]"));
        searchResult.click();

        // 关闭 Appium Session
        driver.quit();
    }
}
